package com.onix.scalecamerapreview.translate;
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.Response;

public class ErrorCodeInterceptor implements Interceptor {
    //查询参数丢失
    public static final int ERROR_CODE_REQUST = 400;
    //系统错误
    public static final int ERROR_CODE_2 = 500;
    public static final int ERROR_CODE_1 = 503;
    public static final int ERROR_CODE_4 = 401;
    public static final int ERROR_CODE_3 = 403;
    //调用太多
    public static final int ERROR_CODE_MORE= 429;


    @Override
    public Response intercept(Chain chain) throws IOException {
        Response response = chain.proceed(chain.request());  //如果401了，会先执行 TokenAuthenticator
        if (response.code() == ERROR_CODE_1||
                response.code() == ERROR_CODE_2||
                response.code() == ERROR_CODE_3||
                response.code() == ERROR_CODE_4
                ){
            throw new ErrorCodeException(response.code(),"系统错误");
        }else if(response.code() == ERROR_CODE_REQUST){
            throw new ErrorCodeException(response.code(),"Make Sure");
        }else if(response.code() == ERROR_CODE_MORE){
            throw new ErrorCodeException(response.code(),"请求太多");
        }
        return response;
    }
}
